package com.book.breakage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.breakage.entity.Back;
import com.book.breakage.mapper.BackMapper;
import com.book.breakage.service.BackService;
import com.book.common.entity.QueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 张高赫
 * @Description:  20173954
 */

@Service
public class BackServiceImpl extends ServiceImpl<BackMapper, Back> implements BackService {
    @Autowired
    BackMapper backMapper;

    @Override
    public List<Back> backList(Back back) {
        QueryWrapper<Back> backQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(back.getPubid())) {
            backQueryWrapper.like("pubid", back.getPubid());
        }
        return this.backMapper.selectList(backQueryWrapper);
    }

    @Override
    public IPage<Back> backPage(QueryRequest request, Back back) {
        QueryWrapper<Back> backQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(back.getBookname())) {
            backQueryWrapper.like("bookname", back.getBookname());
        }
        if (StringUtils.isNotEmpty(back.getPrincipal())) {
            backQueryWrapper.like("principal", back.getPrincipal());
        }
        Page<Back> backPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(backPage, backQueryWrapper);
    }

    @Override
    public void backAdd(Back back) {
        this.save(back);
    }

    @Override
    public void backUpdate(Back back) {
        this.saveOrUpdate(back);
    }

    @Override
    public void backDelete(String[] breReturn) {
        List<String> list = Arrays.asList(breReturn);
        this.removeByIds(list);
    }

    @Override
    public List<Back> backApply(String[] back) {
        List<String> list = Arrays.asList(back);
        return (List<Back>) this.listByIds(list);
    }
}
